// /**
//  * Created by web_developer_02 on 2017/12/1
//  */
//
import areaInfo from './areaInfo'
let cardId = {};

cardId.getAddress=function(cardId){
    if(cardId){
        let index = cardId.substr(0,6);
        let area = areaInfo;
        if(area[index])
            return area[index];
        else return "";
    }else{
        return "";
    }
};

cardId.getSex = function (cardId) {
    return cardId.substr(16, 1) % 2 ? "男" : "女"
};
cardId.getBirthday = function (cardId) {
    let sBirthday = cardId.substr(6, 4) + "-" + Number(cardId.substr(10, 2)) + "-" + Number(cardId.substr(12, 2));
    let d = new Date(sBirthday.replace(/-/g, "/"));
    let myYear = ';' + d.getFullYear() + ';';
    let myMonth = ';' + Math.floor(d.getMonth() + 1) + ';';
    let myDay = ';' + d.getDate() + ';';
    if (myMonth.length < 4) myMonth = '0' + myMonth;
    if (myDay.length < 4) myDay = '0' + myDay;
    let timestr = myYear + '-' + myMonth + '-' + myDay;
    timestr = timestr.replace(/;/g, "");
    return timestr;
};
cardId.getOld = function (cardId) {
    let info = this.getCardIdInfo(cardId);
    return info.old;
};

cardId.isCardId18 = function (cardId) {
    let info = this.getCardIdInfo(cardId);
    return info.isCard;
};
/**
 * 得到身份证相关信息
 * @param {String} cardId
 * @return Object
 */
cardId.getCardIdInfo = function (cardId,datestr) {
    let aCity = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    };
    let iSum = 0;

    let obj = {
        isCard: false,
        msg: "",
        cardId: cardId,
        provinceId: -1,
        provinceName: "其它",
        cardAddress: "其它",
        birthday: "",
        year: -1,
        month: -1,
        day: -1,
        days:-1,
        old: -1,
        sex: "其它"
    };
    if (!/^\d{17}(\d|x)$/i.test(cardId)) {
        obj.isCard = false;
        obj.msg = "你输入的身份证长度或格式错误";
        return obj;
    }
    cardId = cardId.replace(/x$/i, "a");
    if (aCity[parseInt(cardId.substr(0, 2))] === null) {
        obj.isCard = false;
        obj.msg = "你的身份证地区非法";
        return obj;
    }
    let sBirthday = cardId.substr(6, 4) + "-" + Number(cardId.substr(10, 2))
        + "-" + Number(cardId.substr(12, 2));
    let d = new Date(sBirthday.replace(/-/g, "/"));
    let ymdstr = d.getFullYear() + '-' + Math.floor(d.getMonth() + 1) + '-' + d.getDate();
    if (sBirthday !== ymdstr) {
        obj.isCard = false;
        obj.msg = "身份证上的出生日期非法";
        return obj;
    }
    for (let i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(cardId.charAt(17 - i), 11);
    if (iSum % 11 !== 1) {
        obj.isCard = false;
        obj.msg = "你输入的身份证号非法";
        return obj;
    }
    obj.isCard = true;
    obj.msg = "身份证合法";
    obj.provinceId = Math.floor(cardId.substr(0, 2));
    obj.provinceName = aCity[parseInt(obj.provinceId)];
    obj.cardAddress = this.getAddress(cardId);
    let timestr = this.getBirthday(cardId);
    obj.birthday = timestr;
    obj.year = Math.floor(timestr.split('-')[0]);
    obj.month = Math.floor(timestr.split('-')[1]);
    obj.day = Math.floor(timestr.split('-')[2]);
    obj.sex = (cardId.substr(16, 1) % 2 ? "男" : "女");

    datestr = datestr || new Date();
    obj.old = yearMinus(new Date(timestr), new Date(datestr));
    obj.days = Math.floor((new Date(datestr)-new Date(timestr))/(1000 * 60 * 60 * 24));
    function yearMinus(startDate, endDate) {
        let y1 = startDate.getFullYear();
        let m1 = startDate.getMonth() + 1;
        let d1 = startDate.getDate();

        let y2 = endDate.getFullYear();
        let m2 = endDate.getMonth() + 1;
        let d2 = endDate.getDate();

        let year = y2 - y1;
        if (m1 > m2) {
            year--;
        } else if (m1 === m2) {
            if (d1 > d2) {
                year--;
            }
        } else {
        }
        return year;
    }

    // let a=aCity[parseInt(cardId.substr(0,2))]+","+sBirthday+","+(cardId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
    return obj;
};
/**
 * @description 得到随机身份证号码
 * @return {String}
 */
cardId.getRandCardId = function (ymd, address) {
    let coefficientArray = ["7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"];// 加权因子
    let lastNumberArray = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];// 校验码

    let areas = Object.keys(areaInfo);
    let index = Math.floor(Math.random()*areas.length);
    address = address || areas[index];
    // address = address || "420101"; // 住址
    // let birthday = "20160103"; // 生日
    // let index = Math.floor(Math.random() * areas.length);
    let birthday = ymd || getRandYear();

    function getRandYear() {
        let maxY = (new Date()).getFullYear();
        let minY = maxY - 150;
        let y = Math.floor(Math.random() * (maxY - minY + 1) + minY);
        let m = Math.floor(Math.random() * (12 - 1 + 1) + 1);
        let d = 0;
        if ([1, 3, 5, 7, 8, 10, 12].indexOf(m) > -1) {
            d = Math.floor(Math.random() * (31 - 1 + 1) + 1);
        }
        if ([4, 6, 9, 11].indexOf(m) > -1) {
            d = Math.floor(Math.random() * (30 - 1 + 1) + 1);
        }
        if (m === 2) {
            if ((y % 4 === 0 && y % 100 !== 0) || y % 400 === 0) {
                d = Math.floor(Math.random() * (28 - 1 + 1) + 1);
            } else {
                d = Math.floor(Math.random() * (27 - 1 + 1) + 1);
            }
        }
        m = m < 10 ? '0' + m : m;
        d = d < 10 ? '0' + d : d;
        return '' + y + m + d;
    }

    let s = Math.floor(Math.random() * 10).toString() + Math.floor(Math.random() * 10).toString() + Math.floor(Math.random() * 10).toString();

    let array = (address + birthday + s).split("");
    let total = 0;
    for (let i = 0; i < array.length; i++) {
        total = total + parseInt(array[i]) * parseInt(coefficientArray[i]);
    }
    let lastNumber = lastNumberArray[parseInt(total % 11)];
    return address + birthday + s + lastNumber;
};


export default cardId;
